package com.tiger.cloud.service.crm;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tiger.cloud.common.exception.ServiceException;
import com.tiger.cloud.common.mybatis.MybatisTigerDao;
import com.tiger.cloud.common.mybatis.TigerBaseService;
import com.tiger.cloud.common.view.SpringUtils;
import com.tiger.cloud.dao.crm.CommucationDao;
import com.tiger.cloud.dao.crm.CustomerDao;
import com.tiger.cloud.dao.crm.LoanInfoDao;
import com.tiger.cloud.dao.crm.LoanInfoDetailDao;
import com.tiger.cloud.enums.CommucationCustomerType;
import com.tiger.cloud.enums.LoanRecordType;
import com.tiger.cloud.enums.PointCodeEnum;
import com.tiger.cloud.form.CommucationMessage;
import com.tiger.cloud.model.crm.Commucation;
//git.oschina.net/huger/tiger-crm.git
import com.tiger.cloud.model.crm.Customer;
import com.tiger.cloud.model.crm.LoanInfo;
import com.tiger.cloud.model.crm.LoanInfoDetail;
import com.tiger.cloud.model.crm.SystemCommucation;
import com.tiger.cloud.model.share.PointCodeMessage;
import com.tiger.cloud.util.ErrorCode;

/**
 * 客户
 */
@Service("customerService")
public class CustomerService extends TigerBaseService<Customer> implements ICustomerService{
    
    @Autowired
	private CustomerDao customerDao;
    @Autowired
  	private LoanInfoDao loanInfoDao;
    @Autowired
  	private LoanInfoDetailDao loanInfoDetailDao;
    @Autowired
    private CommucationDao commucationDao;
    
	@Override
	public MybatisTigerDao<Customer> getEntityDao() {
		return customerDao;
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean insertCustomer(Customer customer) {
		if(customerDao.insert(customer)>0){
			List<LoanInfoDetail> loanInfoDetails=new ArrayList<LoanInfoDetail>();
			if(customer.getLoanInfos()!=null&&customer.getLoanInfos().size()>0){
				for (LoanInfo loanInfo : customer.getLoanInfos()) {
					loanInfo.setUserId(customer.getUserId());
					loanInfo.setCustomerId(customer.getId());
					loanInfo.setLoanType(LoanRecordType.getEnumByCode(loanInfo.getLoanTypeId().intValue()));
					if(loanInfoDao.insert(loanInfo)>0){
						if(loanInfo.getLoanInfoDetails()!=null&&loanInfo.getLoanInfoDetails().size()>0){
							loanInfo.getLoanInfoDetails().forEach(loanInfoDetail-> {
								loanInfoDetail.setLoanInfoId(loanInfo.getId());
								loanInfoDetail.setLoanType(loanInfo.getLoanType());
								loanInfoDetail.setLoanTypeId(loanInfo.getLoanTypeId());
								loanInfoDetails.add(loanInfoDetail);
							});
						}else{//注意这里是使用添加空数据
							LoanInfoDetail loanInfoDetail=new LoanInfoDetail();
							loanInfoDetail.setLoanInfoId(loanInfo.getId());
							loanInfoDetails.add(loanInfoDetail);
						}
					}else{
						throw new ServiceException(ErrorCode.INSERT_LOAN_INFO_ERROR);
					}
				}
				if(loanInfoDetails!=null&&loanInfoDetails.size()>0){
					int successCount=loanInfoDetailDao.insertBatch(loanInfoDetails);
					if(loanInfoDetails.size()!=successCount){
						throw new ServiceException(ErrorCode.INSERT_LOAN_INFO_DETAIL_ERROR);
					}
				}
			}
			if(SpringUtils.getBean(IUserConsumeService.class).insertRecord(PointCodeEnum.INSERT_CUSTOMER.getCode(), customer.getUserId(), 
				PointCodeMessage.getMsg(PointCodeMessage.INSERT_CUSTOMER,CommonPointRule.getPoint(PointCodeEnum.INSERT_CUSTOMER.getCode()))) !=null){
				SystemCommucation systemCommucation = new SystemCommucation();
				systemCommucation.setUserId(customer.getUserId());
				systemCommucation.setCustomerId(customer.getId());
				systemCommucation.setCustomerType(CommucationCustomerType.CUSTOMER);
				systemCommucation.setContent(CommucationMessage.getMsg(CommucationMessage.ADD_CUSTOMER_MESSAGE,customer.getCustName()));
				if (SpringUtils.getBean(CommucationService.class).insertSystemCommucation(systemCommucation)) {
					return true;
				}
			}
		}
		throw new ServiceException(ErrorCode.INSERT_CUSTOMER_ERROR);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean updateCustomer(Customer customer) {
		if(customerDao.update(customer)>0){
			List<LoanInfoDetail> loanInfoDetails=new ArrayList<LoanInfoDetail>();
			List<Long> loanInfoIds=new ArrayList<Long>();
			for (LoanInfo loanInfo : customer.getLoanInfos()) {
				loanInfo.setUserId(customer.getUserId());
				loanInfo.setCustomerId(customer.getId());
				loanInfoIds.add(loanInfo.getId());
				loanInfo.setLoanType(LoanRecordType.getEnumByCode(loanInfo.getLoanTypeId().intValue()));
				if(loanInfoDao.update(loanInfo)>0){
					if(loanInfo.getLoanInfoDetails()!=null&&loanInfo.getLoanInfoDetails().size()>0){
						loanInfo.getLoanInfoDetails().forEach(loanInfoDetail-> {
							loanInfoDetail.setLoanInfoId(loanInfo.getId());
							if(loanInfoDetail.getId()==null){
								loanInfoDetails.add(loanInfoDetail);
							}else{
								if(loanInfoDetailDao.update(loanInfoDetail)!=1){
									throw new ServiceException(ErrorCode.UPDATE_LOAN_INFO_DETAIL_ERROR);
								}
							}
						});
					}
				}else{
					throw new ServiceException(ErrorCode.UPDATE_LOAN_INFO_ERROR);
				}
				if(loanInfo.getLoanType()==LoanRecordType.NO_MORTGAGE||loanInfo.getLoanType()==LoanRecordType.BRIDGE||loanInfo.getLoanType()==LoanRecordType.ADVANCE_MONEY){
					//注意这里是使用添加空数据
					LoanInfoDetail loanInfoDetail=new LoanInfoDetail();
					loanInfoDetail.setLoanInfoId(loanInfo.getId());
					loanInfoDetails.add(loanInfoDetail);
				}
			}
			if(loanInfoDetails!=null&&loanInfoDetails.size()>0){
				if(loanInfoDetailDao.removeByLoanInfoIds(loanInfoIds)==0){
					throw new ServiceException(ErrorCode.REMOVE_LOAN_INFO_ERROR);
				}
				int successCount=loanInfoDetailDao.insertBatch(loanInfoDetails);
				if(loanInfoDetails.size()!=successCount){
					throw new ServiceException(ErrorCode.INSERT_LOAN_INFO_DETAIL_ERROR);
				}
			}
			return true;
		}
		throw new ServiceException(ErrorCode.UPDATE_CUSTOMER_ERROR);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean deleteCustomer(Customer deleteEntity) {
		if(customerDao.update(deleteEntity)>0){
			Commucation commucation=new Commucation();
			commucation.setUserId(deleteEntity.getUserId());
			commucation.setCustomerId(deleteEntity.getId());
			commucationDao.removeCustomerCommucationByUserId(commucation);
			return true;
		}
		throw new ServiceException(ErrorCode.REMOVE_CUSTOMER_ERROR);
	}
	
	
}